<?
    require_once("mvc/action/Action.php");
    require_once("cms/newsletter/om/NewsletterPeer.php");
    require_once("cms/newsletter/om/NewsletterLanguagePeer.php");

    class EditAction extends Action {

        private $log;

        public function __construct() {
            $this->log =& LoggerManager::getLogger("EditAction");
        }

        public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

            if($form->getNewsletter()->getNewsletterId() > 0) {
                $newsletter = NewsletterPeer::retrieveByPk($form->getNewsletter()->getNewsletterId());
                if(!is_null($newsletter)) {

                    // Languages
                    $codes = array();
                    $c = new Criteria();
                    $c->clearSelectColumns()->clearOrderByColumns();
                    $c->addSelectColumn(NewsletterLanguagePeer::LANGUAGE_CODE);
                    $c->add(NewsletterLanguagePeer::NEWSLETTER_ID, $form->getNewsletter()->getNewsletterId());
                    $rs = NewsletterLanguagePeer::doSelectRS($c);
                    while($rs->next())
                        $codes[] = $rs->getString(1);

                    $form->setLanguages($codes);
                    $form->setNewsletter($newsletter);

                } else {
                    $this->log->error("No newsletter found with id {$form->getNewsletter()->getNewsletterId()}.");
                    $form->getNewsletter()->setNewsletterId("");
                    $form->getNewsletter()->setBody($this->getTemplate());
                }
            } else {
                $form->getNewsletter()->setBody($this->getTemplate($request));
            }

            return $mapping->findForward("success");

        }

        private function getTemplate(Request $request) {

            ob_start();
            include($request->getContext()->getInitParameter(_Newsletter::BODY));
            $template = ob_get_contents();
            ob_end_clean();
            return htmlentities($template);

        }

    }
?>